{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from random import randrange"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def rabin_miller(prime, tests):\n",
    "    if prime < 5:\n",
    "        return prime in [2, 3]\n",
    "    \n",
    "    # set: prime = q * 2**r + 1\n",
    "    q, r = prime - 1, 0\n",
    "    while not q & 1:\n",
    "        q >>= 1\n",
    "        r += 1\n",
    "\n",
    "    # test repeatedly\n",
    "    for _ in range(tests):\n",
    "        a = randrange(2, prime - 1)\n",
    "\n",
    "        # pass if: a**q == 1\n",
    "        x = pow(a, q, prime)\n",
    "        if x in [1, prime - 1]:\n",
    "            continue\n",
    "\n",
    "        # pass if: a**(q * 2**s) == -1, s < r\n",
    "        for _ in range(r - 1):\n",
    "            x = pow(x, 2, prime)\n",
    "            if x == prime - 1:\n",
    "                break\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def prime(bits, tests):\n",
    "    while True:\n",
    "        # random number in [2**bits .. 2**(bits+1)-1]\n",
    "        prime = (1 << bits) | randrange(1 << bits) | 1\n",
    "\n",
    "        # primality test\n",
    "        if rabin_miller(prime, tests):\n",
    "            return prime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## primes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "491"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prime(8, 32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "167539476446412633052018914179671756072742261642391296029084943606975058499651"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prime(256, 32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "327629758798678908807612458298442202698722670823449712916355625086421084819056655501101117430544648756451802433910857072317916389191167555508482465929724915679965943104133398992501561113977009936622055728794693540394304838984423959080924829552190406356895643910247318370440605397978334769846807883781807340317"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prime(1024, 32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
